"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 防篡改
Case Name   : 数据库实例数据目录及文件权限看护
Create At   : 2022/3/25
Owner       : opentestcase012
Description :
    分别对四个文件[postgresql.conf,pg_hba.conf,postmaster.opts,pg_ident.conf]执行1-3语句
    1.文件看护
    find @INSTANCE_PATH@ -iname [文件名] -exec ls -l {} ';'
    2.文件权限不等于600
    find @INSTANCE_PATH@ -iname [文件名] -exec ls -l {} ';'
    | grep -v "\-rw-------"
    3.文件权限不等于600的数量
    find @INSTANCE_PATH@ -iname [文件名] -exec ls -l {} ';'
    |grep -v "\-rw-------"|wc -l
    4.实例数据目录权限不等于700
    ls -lR @INSTANCE_PATH@ grep "^d\" | grep -v "drwx------"
    5.实例数据目录权限不等于700数量统计
    ls -lR @INSTANCE_PATH@ grep "^d" | grep -v "drwx------"|wc -l
    6.实例数据目录下的文件权限不等于600
    ls -lR @INSTANCE_PATH@ grep "^\-"  | grep -v "\-rw-------"
    7.实例数据目录下的文件权限不等于600数量统计
    ls -lR @INSTANCE_PATH@ grep "^\-"  | grep -v "\-rw-------"|wc -l"
Expect      :
    1.成功
    2.执行结果为空
    3.执行结果为0
    4.执行结果为空
    5.执行结果为0
    6.执行结果为空
    7.执行结果为0
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Security(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node(node='PrimaryDbUser')
        self.common = Common()

    def test_security(self):
        files = [macro.DB_PG_CONFIG_NAME, macro.PG_HBA_FILE_NAME,
                 'postmaster.opts', 'pg_ident.conf']
        for file in files:
            text = f'-----step1：{file}文件看护 expect:成功-----'
            self.log.info(text)
            cmd = f"find {macro.DB_INSTANCE_PATH} -iname {file} " \
                f"-exec  ls  -l  {{{{}}}} ';' "
            self.log.info(cmd)
            msg = self.common.get_sh_result(self.pri_node, cmd)
            self.assertIn(file, msg, '执行失败:' + text)

            text = f'-----step2：{file}文件权限不等于600 expect:执行结果为空-----'
            self.log.info(text)
            cmd = f"find {macro.DB_INSTANCE_PATH} -iname {file} " \
                f"-exec  ls  -l  {{{{}}}} ';'  | grep -v '\-rw-------' "
            self.log.info(cmd)
            msg = self.common.get_sh_result(self.pri_node, cmd)
            self.assertEqual('', msg, '执行失败:' + text)

            text = f'-----step3：{file}文件权限不等于600的数量 expect:执行结果为0-----'
            self.log.info(text)
            cmd = f"find {macro.DB_INSTANCE_PATH} -iname {file} " \
                f"-exec  ls  -l  {{{{}}}} ';' |grep -v '\-rw-------'|wc -l "
            self.log.info(cmd)
            msg = self.common.get_sh_result(self.pri_node, cmd)
            self.assertEqual('0', msg, '执行失败:' + text)

        text = f'-----step4：实例数据目录权限不等于700 expect:执行结果为空-----'
        self.log.info(text)
        cmd = f"ls -lR {macro.DB_INSTANCE_PATH} | grep '^d' | " \
            f"grep -v 'drwx------' "
        self.log.info(cmd)
        msg = self.common.get_sh_result(self.pri_node, cmd)
        self.assertEqual('', msg, '执行失败:' + text)

        text = f'-----step5：实例数据目录权限不等于700数量统计 expect:执行结果为0-----'
        self.log.info(text)
        cmd = f"ls -lR {macro.DB_INSTANCE_PATH} | grep '^d' | " \
            f"grep -v 'drwx------' |wc -l "
        self.log.info(cmd)
        msg = self.common.get_sh_result(self.pri_node, cmd)
        self.assertEqual('0', msg, '执行失败:' + text)

        text = f'-----step6：实例数据目录下的文件权限不等于600 expect:执行结果为空-----'
        self.log.info(text)
        cmd = f"ls -lR {macro.DB_INSTANCE_PATH} | grep '^\-' | " \
            f"grep -v '\-rw-------' "
        self.log.info(cmd)
        msg = self.common.get_sh_result(self.pri_node, cmd)
        self.assertEqual('', msg, '执行失败:' + text)

        text = f'-----step7：实例数据目录下的文件权限不等于600数量统计 expect:执行结果为0-----'
        self.log.info(text)
        cmd = f"ls -lR {macro.DB_INSTANCE_PATH} | grep '^\-' | " \
            f"grep -v '\-rw-------' |wc -l "
        self.log.info(cmd)
        msg = self.common.get_sh_result(self.pri_node, cmd)
        self.assertEqual('0', msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----无需清理环境-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
